package com.hycc.base;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

public class TtCallable {
    public static void main(String[] args) {
        MyCallable myCallable = new MyCallable();

        new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println("实现Runnable接口");
            }
        }).start();

        new Thread(new FutureTask<String>(myCallable){
            @Override
            public void run() {
                System.out.println("FutureTask适配类接收Callable接口实现类");
            }
        }).start();

        FutureTask<String> task = new FutureTask<>(myCallable);
        new Thread(task).start();

        //接收返回值 task.get()
        String s = null;
        try {
            s = task.get();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
        System.out.println(s);
    }
}

class MyCallable implements Callable<String>{
    @Override
    public String call() throws Exception {
        System.out.println("实现Callable接口的Call方法");
        return "called";
    }
}